package com.bjsc.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bjsc.entity.Withdraw;
import com.bjsc.enums.BooleanEnum;
import com.bjsc.mapper.UserMapper;
import com.bjsc.mapper.WithdrawMapper;
import com.bjsc.service.IWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author create by VIC.Huang
 * @date 2018/2/7
 */
@Service
public class WithdrawServiceImpl extends ServiceImpl<WithdrawMapper, Withdraw> implements IWithdrawService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<Map<Object, Object>> selectWithdrawPage(Page<Map<Object, Object>> page) {
        page.setRecords(baseMapper.selectWithdrawList(page));
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean settle(Long id) {
        Withdraw entity = baseMapper.selectById(id);
        if(null != entity && (BooleanEnum.NO.getCode()).equals(entity.getHandleStatus())) {
            entity.setHandleStatus(BooleanEnum.YES.getCode());
            if(baseMapper.updateById(entity) > 0) {
                userMapper.updateUserFrozenAsset(entity.getUserId(), entity.getMoney().negate());
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cancelSettle(Long id) {
        Withdraw entity = baseMapper.selectById(id);
        if(null != entity && (BooleanEnum.YES.getCode()).equals(entity.getHandleStatus())) {
            entity.setHandleStatus(BooleanEnum.NO.getCode());
            if(baseMapper.updateById(entity) > 0) {
                userMapper.updateUserFrozenAsset(entity.getUserId(), entity.getMoney());
                return true;
            }
        }
        return false;
    }
}
